package com.llk.myIoLearn;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

public class Client {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket();
        socket.connect(new InetSocketAddress("127.0.0.1", 2333));
        System.out.println("成功连接服务端");

        // 发送数据
        String sendData = "hello server2!";
        OutputStream os = socket.getOutputStream();
        os.write(sendData.getBytes());
        socket.shutdownOutput(); // 关键！通知服务端数据发送完毕[5](@ref)

        // 接收响应
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(socket.getInputStream()))) {
            StringBuilder response = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            System.out.println("完整响应: " + response);
        }


    }
}
